<?php

/**
 * @Author:      lyt
 * @DateTime:    2016-04-19 09:37:21
 * @Description: 用于门店耗材的操作
 */
class ScsSupplyController extends BaseController
{   

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-01-09T15:47:36+0800
     * @return   [type]                   [description]
     */
    public function supply_brand(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',                
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            $name=Input::get("name");
            $status=Input::get("status");            
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $sdb=DataSupplyBrand::orderBy("id","asc");
            if($status!=""){
                $sdb->where("status",$status);
            }
            if($name){
                $sdb->where("brand_name", 'like', '%' . $name . '%');
            }
            $count=$sdb->count();
            $sdb=$sdb->select("id","brand_name")->skip($page_size*($page-1))->take($page_size)->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"supply_brand"=>$sdb));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "supply_brand", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-01-09T16:02:32+0800
     * @return   [type]                   [description]
     */
    public function supply_series(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',                
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            $name=Input::get("name");
            $status=Input::get("status");  
            $brand_id=Input::get("brand_id");                        
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $sdb=DataSupplySeries::orderBy("id","asc");

            if($brand_id){
                $sdb->where("brand_id",$brand_id);
            }
            if($status!=""){
                $sdb->where("status",$status);
            }
            if($name){
                $sdb->where("name", 'like', '%' . $name . '%');
            }
            $count=$sdb->count();
            $sdb=$sdb->select("id","name")->skip($page_size*($page-1))->take($page_size)->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"supply_series"=>$sdb));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "supply_series", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-01-09T16:14:03+0800
     * @return   [type]                   [description]
     */
    public function supply_unit(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',                
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            $name=Input::get("name");
            $status=Input::get("status");                     
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $sdb=DataSupplyUnit::orderBy("id","asc");

            if($status!=""){
                $sdb->where("status",$status);
            }
            if($name){
                $sdb->where("name", 'like', '%' . $name . '%');
            }
            $count=$sdb->count();
            $sdb=$sdb->select("id","name")->skip($page_size*($page-1))->take($page_size)->get();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"supply_unit"=>$sdb));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "supply_unit", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-02-22 11:30:48
     * @Description: 用于 门店耗材的列表
     */
    public function shopsupply(){
    	$validator = Validator::make(Input::all(),
            array(
                'token' => 'required',                
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
    	do{
    		if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $page_size=Input::get("page_size",10);
            $page=Input::get("page",1);
            $status=Input::get("status");  
            $brand_id=Input::get("brand_id");   
            $one_id=Input::get("one_id");
            $two_id=Input::get("two_id");
            $three_id=Input::get("three_id");   
            $car_retrieval_code=Input::get("car_retrieval_code");
            $type=Input::get("type");
            $search_retrieval_code=Input::get("retrieval_code");
            $car_str=Input::get("car_str");
            $anhui_code =  Input::get("anhui_code");
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            if($type||$search_retrieval_code||$car_str){
                $ids=SupplyItem::retrievalCode($car_retrieval_code,$type,$search_retrieval_code,$car_str);//获得标准的耗材id范围
            }
            $sins=SupplyItem::
                leftJoIn("anhui_data.supply_brand","supply_brand.id","=","supply_item.brand_id")->
                leftJoIn("anhui_data.system_users","system_users.id","=","supply_item.updated_by")->
                leftJoIn("anhui.sc_user","sc_user.id","=","supply_item.updated_by")->
                leftJoIn("anhui.supply_classification as sc3","sc3.id","=","supply_item.supply_classification_id")->
                leftJoIn("anhui.supply_classification as sc2","sc2.id","=","sc3.parent_id")->
                leftJoIn("anhui.supply_classification as sc1","sc1.id","=","sc2.parent_id")->orderby("supply_item.id","asc")->where("type",1);

            if(isset($ids)){
                $sins->whereIn("supply_item.id",$ids);
            }
            if($brand_id){
                $sins->where("supply_item.brand_id", $brand_id);
            }
            if($status!=""){
                $sins->where("supply_item.status",$status);
            }            
            if($three_id){
                $sins->where("sc3.id",$three_id);
            }else if($two_id){
                $sins->where("sc2.id",$two_id);
            }else if($one_id){
                $sins->where("sc1.id",$one_id);
            }
            if ($anhui_code !="") $sins->where("supply_item.anhui_code",'like', '%' . $anhui_code . '%');
            $count=$sins->count();
            $sins=$sins->select("supply_item.id","supply_item.anhui_code","supply_item.name","supply_brand.brand_name","sc3.name as sc3_name","sc2.name as sc2_name","sc1.name as sc1_name","system_users.name as data_user_name","sc_user.real_name as sc_user_name","supply_item.updated_type","supply_item.updated_at","supply_item.updated_by","supply_item.type","supply_item.status","supply_item.code")->skip($page_size*($page-1))->take($page_size)->get();
            // foreach($sins as $_sins){
            //      $sl=SupplyLocation::where('item_id',$_sins->id)->where('shop_id',$sc_user->shop_id)->where('item_type',1)->first();
            //      $_sins->location=isset($sl->location) ? $sl->location : "";
            // }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("count"=>$count,"supply_info"=>$sins));
    	}while(false);
    	Log::info("info", array("system" => "scs" ,"event" => "shopsupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }



    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-01-10T13:13:12+0800
     * @return   [type]                   [description]
     */
    public function createsupply(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'brand_id' => 'required',
                'model' => 'required|max:100',
                'unit_id' => 'required',
                'sale_unit_id' => 'required',
                'volume_size' => 'required',
                'code' => 'required',
                'name' => 'required',
                'supply_classification_id' => 'required',
                'status' => 'required|in:1,0',      
                'location' => 'required',   
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $_sins=SupplyItem::where('brand_id',Input::get('brand_id'))->where('series_id',Input::get('series_id'))->where('code',Input::get('code'))->where('volume_size',Input::get('volume_size'))->where('unit_id',Input::get('unit_id'))->first();
            if($_sins){
                $response = Lang::get("scs_error.supply_exit_error_10005");
                break;
            }
            $brand=DataSupplyBrand::find(Input::get("brand_id")); //检查品牌状态
            if(is_null($brand)){
                $response = Lang::get("scs_error.supply_brand_error_41012");
                break;
            }
            if($brand->status!=1){
                $response = Lang::get("scs_error.supply_brand_error_41013");
                break;
            }
            if(Input::get("series_id")){//检查系列状态
                $series=DataSupplySeries::find(Input::get("series_id")); 
                if(is_null($series)){
                    $response = Lang::get("scs_error.supply_series_error_41014");
                    break;
                }
                if($series->status!=1){
                    $response = Lang::get("scs_error.supply_series_error_41015");
                    break;
                }
            }
            $scf=SupplyClassification::find(Input::get("supply_classification_id")); //耗材三级分类
            if(is_null($scf)){
                $response = Lang::get("scs_error.supply_class_error_41016");
                break;
            }
            if($scf->status!=1){
                $response = Lang::get("scs_error.supply_class_error_41017");
                break;
            }
            if($scf->level!=3){
                $response = Lang::get("scs_error.supply_class_error_41018");
                break;
            }
            $si=new SupplyItem;
            $si->brand_id=$brand->id;
            $si->brand_name=$brand->brand_name;
            $si->series_id=isset($series)?$series->id:0;
            $si->series_name=isset($series)?$series->name:"";
            $si->model=Input::get("model");
            $si->status=Input::get("status");
            $si->type=1;
            $si->shop_id=$sc_user->shop_id;
            $si->unit_id=Input::get("unit_id");
            $si->sale_unit_id=Input::get("sale_unit_id");
            $si->volume_size=Input::get("volume_size");
            $si->code=Input::get("code");
            $si->old_code=Input::get("old_code");
            $si->name=Input::get("name");
            $si->supply_classification_id=Input::get("supply_classification_id");
            $si->texture_material=Input::get("texture_material");
            $si->colour=Input::get("colour");
            $si->desc=Input::get("desc");
            $si->bar_code=Input::get("bar_code");            

            $si->quality_guarantee_period=null;
            $si->weight=null;
            $si->length=null;
            $si->width=null;
            $si->height=null;
            
            $si->car_model_comments=Input::get("car_model_comments");
            $si->created_type='sc';
            $si->created_by=$sc_user->id;
            $si->updated_type='sc';
            $si->updated_by=$sc_user->id;
            
            $fenlei=SupplyClassification::leftJoIn("supply_classification as sc","sc.id","=","supply_classification.parent_id")->
            leftJoIn("supply_classification as sc2","sc2.id","=","sc.parent_id")->select("supply_classification.retrieval_code","supply_classification.name","sc.retrieval_code as two_rc","sc.name as two_name","sc2.retrieval_code as one_rc","sc2.name as one_name")->where("supply_classification.id",$si->supply_classification_id)->first(); 
            $retrieval_code=$fenlei->one_rc.$fenlei->two_rc.$fenlei->retrieval_code.$fenlei->one_name.$fenlei->two_name.$fenlei->name.$si->brand_name.$si->series_name.$si->model.$si->code.$si->old_code.$si->car_model_comments.$si->name;
            $si->retrieval_code=$retrieval_code;
            $si->save();
            $max = DB::table('anhui.supply_item')->max('anhui_code');
            $si->anhui_code = $max + 1;
            $si->save();

            $sl=new SupplyLocation;
            $sl->item_id=$si->id;
            $sl->item_type=1;
            $sl->shop_id=$sc_user->shop_id;
            $sl->location=Input::get('location');
            $sl->save();
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("supply_item_id"=>$si->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "createsupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /**
     * @Author   lyt                      <ahlyt@anhuibj.com>
     * @DateTime 2018-01-10T14:24:41+0800
     * @return   [type]                   [description]
     */
    public function supplydetail(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required', 
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            
            $su = SupplyItem::
                leftJoIn("supply_classification as sc3","sc3.id","=","supply_item.supply_classification_id")->
                leftJoIn("supply_classification as sc2","sc2.id","=","sc3.parent_id")->
                leftJoIn("supply_classification as sc1","sc1.id","=","sc2.parent_id")->
                leftJoIn("anhui_data.supply_unit as su1","su1.id","=","supply_item.unit_id")->
                leftJoIn("anhui_data.supply_unit as su2","su2.id","=","supply_item.sale_unit_id")->where("supply_item.id", Input::get("id"));
            $su = $su->select('supply_item.*', 'sc3.name as sc_3_name','sc3.id as sc_3_id', 'sc2.id as sc_2_id','sc2.name as sc_2_name', 'sc1.name as sc_1_name','sc1.id as sc_1_id','su1.name as unit_name','su2.name as sale_unit_name')->first();
            if(is_null($su)||$su->type==0){
                $response = Lang::get("scs_error.supply_item_not_exit_41000");
                break;
            }
          
            $sl=SupplyLocation::where('item_id',$su->id)->where('shop_id',$sc_user->shop_id)->first();
            $su->location=isset($sl->location) ? $sl->location : "";

            $response = array('code' => 0, 'msg' => 'ok','result' => array('supply_item_info' => $su));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "supplydetail", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


     public function updatesupply(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'item_id'=> 'required',
                'brand_id' => 'required',
                'model' => 'required|max:100',
                'unit_id' => 'required',
                'sale_unit_id' => 'required',
                'volume_size' => 'required',
                'name' => 'required',
                'status' => 'required|in:1,0',      
                'location' => 'required',   
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("scs_error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $sc_user=$this->verificationSCSUserToken(Input::get("token"),$response);
            if(is_null($sc_user)){
                break;
            }
            $item_id=Input::get('item_id');
            $si=SupplyItem::find(Input::get('item_id'));
            if(is_null($si)||$si->type==0){
                $response = Lang::get("scs_error.supply_item_not_exit_41000");
                break;
            }
            // if($sc_user->shop_id !=$si->shop_id){
            //     $response = Lang::get("scs_error.supply_item_source_error_41006");
            //     break;
            // }
            $_sins=SupplyItem::where('id','!=',Input::get('item_id'))->where('brand_id',Input::get('brand_id'))->where('series_id',Input::get('series_id'))->where('code',$si->code)->where('volume_size',Input::get('volume_size'))->where('unit_id',Input::get('unit_id'))->first();
            if($_sins){
                $response = Lang::get("scs_error.supply_exit_error_10005");
                break;
            }
            $brand=DataSupplyBrand::find(Input::get("brand_id")); //检查品牌状态
            if(is_null($brand)){
                $response = Lang::get("scs_error.supply_brand_error_41012");
                break;
            }
            if($brand->status!=1){
                $response = Lang::get("scs_error.supply_brand_error_41013");
                break;
            }
            if(Input::get("series_id")){//检查系列状态
                $series=DataSupplySeries::find(Input::get("series_id")); 
                if(is_null($series)){
                    $response = Lang::get("scs_error.supply_series_error_41014");
                    break;
                }
                if($series->status!=1){
                    $response = Lang::get("scs_error.supply_series_error_41015");
                    break;
                }
            }
            
    
            $si->brand_id=$brand->id;
            $si->brand_name=$brand->brand_name;
            $si->series_id=isset($series)?$series->id:0;
            $si->series_name=isset($series)?$series->name:"";
            $si->model=Input::get("model");
            $si->status=Input::get("status");
            $si->shop_id=$sc_user->shop_id;
            $si->unit_id=Input::get("unit_id");
            $si->sale_unit_id=Input::get("sale_unit_id");
            $si->volume_size=Input::get("volume_size");
            $si->old_code=Input::get("old_code");
            $si->name=Input::get("name");
            $si->texture_material=Input::get("texture_material");
            $si->colour=Input::get("colour");
            $si->desc=Input::get("desc");
            $si->bar_code=Input::get("bar_code");
            
            $si->car_model_comments=Input::get("car_model_comments");
            $si->updated_type='sc';
            $si->updated_by=$sc_user->id;
            
            $fenlei=SupplyClassification::leftJoIn("supply_classification as sc","sc.id","=","supply_classification.parent_id")->
            leftJoIn("supply_classification as sc2","sc2.id","=","sc.parent_id")->select("supply_classification.retrieval_code","supply_classification.name","sc.retrieval_code as two_rc","sc.name as two_name","sc2.retrieval_code as one_rc","sc2.name as one_name")->where("supply_classification.id",$si->supply_classification_id)->first(); 
            $retrieval_code=$fenlei->one_rc.$fenlei->two_rc.$fenlei->retrieval_code.$fenlei->one_name.$fenlei->two_name.$fenlei->name.$si->brand_name.$si->series_name.$si->model.$si->code.$si->old_code.$si->car_model_comments;
            $si->retrieval_code=$retrieval_code;
            $si->save();
            
            $sl=SupplyLocation::where('item_id',Input::get('item_id'))->where('shop_id',$sc_user->shop_id)->first();
            if($sl){
                $sl->item_type=$si->type;
                $sl->location=Input::get("location");
                $sl->save();
            }else{
                $sl=new SupplyLocation;
                $sl->item_id=$si->id;
                $sl->item_type=$si->type;
                $sl->shop_id=$sc_user->shop_id;
                $sl->location=Input::get("location");
                $sl->save();
            }           
            Cache::forget('supply_item_'.Input::get('item_id'));
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$si->id));
        }while(false);
        Log::info("info", array("system" => "scs" ,"event" => "updatesupply", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


   
}